package com.budwk.app.sqfwV2.generator.patrolAndVisit;


import cn.hutool.json.JSONUtil;
import com.budwk.app.sqfw.models.Sqfw_formSummary_new;
import com.budwk.app.sqfw.services.SqfwFormsummaryNewService;
import com.budwk.app.sqfwDoc.codeGenerator.pdfGenerate.SqfwPdfCreateCommonBO;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.BusinessParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.CompleteFlowBusinessParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.ExcelGenerateParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.ExcelRowDataParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.generate.BaseExcelGenerator;
import com.budwk.app.sqfwDoc.codeGeneratorV2.generate.ResultOfExcelDataInto;
import com.budwk.app.sqfwV2.dto.PropagandaScreeningDTO;
import com.budwk.app.sqfwV2.dto.dto.AuthenticRightDTO;
import com.budwk.app.sqfwV2.dto.dto.DeptDTO;
import com.budwk.app.sqfwV2.dto.image.AuthenticRightConfigBo;
import com.budwk.app.sqfwV2.enums.AuthenticRightEnum;
import com.budwk.app.sqfwV2.enums.PatrolAndVisitEnum;
import com.budwk.app.util.poiUtils.excelPictureConfigBO.ExcelFileCreateParamBO;
import com.budwk.app.util.poiUtils.nutzBeanUtils.NutzIocUtils;
import org.nutz.dao.Cnd;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Collectors;

import static com.budwk.app.util.poiUtils.CommonFormOfficeUtils.addPictureToByteList;

public class PatrolAndVisitExcelGenerator extends BaseExcelGenerator {

    private String templateName;



    public PatrolAndVisitExcelGenerator(){}

    public PatrolAndVisitExcelGenerator(String templateName){
        this.templateName=templateName;
    }





    public boolean pdfCreateConditionCheck(SqfwPdfCreateCommonBO pdfDTO) {
        return true;
    }


    @Override
    protected void customerPopularHeaderData(Map<String, Object> excelParam, ExcelGenerateParam excelGenerateParam) {

    }

    @Override
    protected void customerPopularHeaderDataForAdmin(Map<String, Object> excelParam, ExcelGenerateParam excelGenerateParam) {

    }

    @Override
    protected ResultOfExcelDataInto judgeExcelHaveDataBeforeErgodic(Map<String, Object> excelParam, ExcelGenerateParam excelGenerateParam) {
        return null;
    }

    @Override
    protected boolean addRowDataCondition(ExcelRowDataParam rowDataParam, ExcelGenerateParam excelGenerateParam) {
        return true;
    }

    @Override
    protected void customerPopularSheetData(List<Map<String, Object>> listMap, ExcelGenerateParam excelGenerateParam) {

    }

    @Override
    protected void customerPopularRowDataMap(Map<String, Object> rowDataMap, ExcelRowDataParam rowDataParam) {
        CompleteFlowBusinessParam businessParam = (CompleteFlowBusinessParam) rowDataParam.getBusinessParam();
        SqfwFormsummaryNewService sqfwFormsummaryNewService = NutzIocUtils.getBean(SqfwFormsummaryNewService.class);
        Sqfw_formSummary_new sqfwFormSummaryNew =   sqfwFormsummaryNewService.fetch((Cnd.where(Sqfw_formSummary_new::getSqfwId,"=", businessParam.getSqfwId())));
        Integer publicityRecord = 0;
        Integer searchRecord = 0;
        List<Sqfw_formSummary_new> publicityRecordList = sqfwFormsummaryNewService.query((Cnd.where(Sqfw_formSummary_new::getSqfwId,"=", businessParam.getSqfwId()))
                .and("param_data->>'type'","=",PatrolAndVisitEnum.A.getName()));
        for (Sqfw_formSummary_new sqfw_formSummary_new : publicityRecordList) {
            PropagandaScreeningDTO propagandaScreeningDTO=   JSONUtil.toBean(sqfw_formSummary_new.getParamData(),PropagandaScreeningDTO.class) ;
            publicityRecord = publicityRecord + Arrays.asList( propagandaScreeningDTO.getTheme().split(",")).size();
        }

        List<Sqfw_formSummary_new> searchRecordList = sqfwFormsummaryNewService.query((Cnd.where(Sqfw_formSummary_new::getSqfwId,"=", businessParam.getSqfwId()))
                .and("param_data->>'type'","=",PatrolAndVisitEnum.B.getName()));
        for (Sqfw_formSummary_new sqfw_formSummary_new : searchRecordList) {
            PropagandaScreeningDTO propagandaScreeningDTO=   JSONUtil.toBean(sqfw_formSummary_new.getParamData(),PropagandaScreeningDTO.class) ;
            searchRecord = searchRecord + Arrays.asList( propagandaScreeningDTO.getTheme().split(",")).size();
        }

        Integer peaceAndCare = sqfwFormsummaryNewService.count((Cnd.where(Sqfw_formSummary_new::getSqfwId,"=", businessParam.getSqfwId()))
                .and("param_data->>'type'","=",PatrolAndVisitEnum.C.getName()));
        rowDataMap.put("community", sqfwFormSummaryNew.getCommunity().equals("直属")?sqfwFormSummaryNew.getCounty():sqfwFormSummaryNew.getCommunity());
        rowDataMap.put("publicityRecord",publicityRecord );
        rowDataMap.put("searchRecord", searchRecord);
        rowDataMap.put("peaceAndCare", peaceAndCare);
    }

    @Override
    protected void customerPopularRowDataMapForAdmin(Map<String, Object> rowDataMap, ExcelRowDataParam rowDataParam) {

    }

    @Override
    protected ResultOfExcelDataInto judgeExcelHaveDataBeforeCreate(Map<String, Object> excelParam, ExcelGenerateParam excelGenerateParam) {
        return null;
    }

    @Override
    protected String getTemplatePath(ExcelGenerateParam excelGenerateParam) {
        return this.templateName;
    }

    @Override
    protected String getDownloadFileName(ExcelGenerateParam excelGenerateParam) {
        String code = excelGenerateParam.getExcelConfig().getFileIdentity();
        return PatrolAndVisitEnum.getFileName(Integer.parseInt(code));
    }

    @Override
    protected Class<?> getImageConfigBO() {
        return AuthenticRightConfigBo.class;

    }

    @Override
    protected void completeExcelFileCreateParam(ExcelFileCreateParamBO excelFileCreateParamBO) {

    }


}
